﻿using System;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Emit;
using System.Reflection;
using System.Data;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.Net;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, Guid> dic = new Dictionary<string, Guid>
            {
                { "233", Guid.NewGuid() },
                { "666", Guid.NewGuid() }
            };

            var v = dic.FirstOrDefault(a => a.Key == "123");

            UnZip(@"E:\\IA\Desktop\新建文件夹\金地二期.zip", @"E:\\IA\Desktop\新建文件夹\");
            Console.WriteLine("Hello World!");
            Console.ReadKey();
        }

        /// <summary>
        /// ZIP:解压一个zip文件
        /// add yuangang by 2016-06-13
        /// </summary>
        /// <param name="ZipFile">需要解压的Zip文件（绝对路径）</param>
        /// <param name="TargetDirectory">解压到的目录</param>
        /// <param name="Password">解压密码</param>
        /// <param name="OverWrite">是否覆盖已存在的文件</param>
        public static void UnZip(string ZipFile, string TargetDirectory, bool OverWrite = true)
        {
            //如果解压到的目录不存在，则报错
            if (!Directory.Exists(TargetDirectory))
            {
                throw new FileNotFoundException("指定的目录: " + TargetDirectory + " 不存在!");
            }
            //目录结尾
            TargetDirectory = TargetDirectory.TrimEnd('\\') + "\\";

            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            ZipStrings.CodePage = Encoding.GetEncoding("GBK").CodePage;
            using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile)))
            {
                ZipEntry theEntry;

                while ((theEntry = zipfiles.GetNextEntry()) != null)
                {
                    string directoryName = "";
                    string pathToZip = "";
                    pathToZip = theEntry.Name;

                    if (!string.IsNullOrWhiteSpace(pathToZip))
                    {
                        directoryName = Path.GetDirectoryName(pathToZip) + "\\";
                    }

                    string fileName = Path.GetFileName(pathToZip);

                    Directory.CreateDirectory(TargetDirectory + directoryName);

                    if (string.IsNullOrWhiteSpace(fileName)) continue;

                    bool fileExists = File.Exists(TargetDirectory + directoryName + fileName);
                    if (fileExists && !OverWrite) continue;

                    using (FileStream streamWriter = File.Create(TargetDirectory + directoryName + fileName))
                    {
                        int size = 2048;
                        byte[] data = new byte[2048];
                        while ((size = zipfiles.Read(data, 0, data.Length)) > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        streamWriter.Close();
                    }
                }

                zipfiles.Close();
            }
        }

        public class Entity
        {
            public string Name { get; set; }
        }
    }
}
